package com.wuli.food.generator;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.wuli.food.generator.constant.MysqlCodeGeneratorConstant;

/**
 * 代码生成
 * 
 * @author xqq
 *
 */
public class CodeGenerator {

	/**
	 * 全局配置构造
	 * 
	 * @param builder
	 */
	private static void builderGlobalConfig(GlobalConfig.Builder builder) {
		//指定date类型格式
		builder.dateType(DateType.ONLY_DATE);
		builder.author("xiongqiaoqiao").fileOverride().outputDir("d://MpGenerator");
	}

	/**
	 * 构造策略配置项一般用于controller service serviceimp entity
	 * 
	 * @param builder
	 */
	private static void builderStrategyConfig(StrategyConfig.Builder builder) {
		builder.entityBuilder()
				// 开启lombok
				.enableLombok()
				// 命名策略
				.naming(NamingStrategy.underline_to_camel)
				// id生成策略
				.idType(IdType.ASSIGN_UUID)

//				.superClass(BaseEntity.class)
//				.addIgnoreColumns("create_time", "modify_time", "create_user_id", "modify_user_id");
		;
		builder.controllerBuilder().enableRestStyle();

		builder.serviceBuilder().formatServiceFileName("%sService").formatServiceImplFileName("%sServiceImpl");
		builder.addInclude(new String[] {"food", "type"});
	}

	/**
	 * 包配置构造
	 * 
	 * @param builder
	 */
	private static void builderPackageConfig(PackageConfig.Builder builder) {
		builder.controller(MysqlCodeGeneratorConstant.CONTROLLER_PACKAGE_NAME)
				.entity(MysqlCodeGeneratorConstant.ENTITY_PACKAGE_NAME)
				.mapper(MysqlCodeGeneratorConstant.MAPPER_PACKAGE_NAME)
				.xml(MysqlCodeGeneratorConstant.XML_PACKAGE_NAME)
				.service(MysqlCodeGeneratorConstant.SERVICE_PACKAGE_NAME)
				.serviceImpl(MysqlCodeGeneratorConstant.SERVICEIMPL_PACKAGE_NAME).parent(null);
	}

	public static void main(String[] args) {
		FastAutoGenerator.create(MysqlCodeGeneratorConstant.MYSQL_URL, MysqlCodeGeneratorConstant.USERNAME,
				MysqlCodeGeneratorConstant.PASSWORD).globalConfig(builder -> {
					builderGlobalConfig(builder);
				}).packageConfig(builder -> {
					builderPackageConfig(builder);

				}).strategyConfig(builder -> {
					builderStrategyConfig(builder);
				}).execute();
		;

	}
}
